import getAjax from "@/util/ajax";
import getSearchData from "@/util/dataUtil";
import secretUtil from "./secretUtil";

export default {
  search(_this, platform, keyWord, currentPage, pageSize) {
    if (!keyWord) {
      _this.$message.warning("请先输入歌曲或歌手名称！");
      return;
    }
    let loading = _this.$loading({
      target: "#dataTable",
      text: "正在加载数据中...",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.5)"
    });
    let ajax = getAjax();
    let url = `/${platform}Search?`;
    let headers = {};
    switch (platform) {
      case "kg":
        url += `keyword=${encodeURIComponent(
          keyWord
        )}&page=${currentPage}&pagesize=${pageSize}&showtype=10&plat=2&version=7910&tag=1&correct=1&privilege=1&sver=5`;
        break;
      case "kw":
        url += `client=kt&all=${encodeURIComponent(keyWord)}&pn=${currentPage -
        1}&rn=${pageSize}&uid=794762570&ver=kwplayer_ar_9.2.2.1&vipver=1&show_copyright_off=1&newver=1&ft=music&cluster=0&strategy=2012&encoding=utf8&rformat=json&vermerge=1&mobi=1&issubtitle=1`;
        break;
      case "tx":
        url += `ct=24&qqmusic_ver=1298&remoteplace=txt.yqq.top&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=${currentPage}&n=${pageSize}&w=${encodeURIComponent(
          keyWord
        )}&cv=4747474&ct=24&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&uin=0&hostUin=0&loginUin=0`;
        break;
      case "mg":
        url += `ua=Android_migu&version=5.0.1&text=${encodeURIComponent(
          keyWord
        )}&pageNo=${currentPage}&pageSize=${pageSize}&searchSwitch=%7B%22song%22%3A1%2C%22album%22%3A0%2C%22singer%22%3A0%2C%22tagSong%22%3A0%2C%22mvSong%22%3A0%2C%22songlist%22%3A0%2C%22bestShow%22%3A1%7D`;
        headers = {
          osVersion: "android 7.0",
          "User-Agent": "okhttp/3.9.1"
        };
        break;
      default:
        break;
    }
    ajax
      .get(url, { headers: headers })
      .then(response => {
        if (response.status === 200) {
          let newData = getSearchData(platform, response.data);
          _this.tableData = newData.data;
          _this.totalRows = newData.total;
        }
      })
      .catch(reason => {
        _this.$message.error("请求失败");
        console.log(reason);
      });
    loading.close();
  },
  getSongUrl(_this, platform, songInfo, hash, type) {
    let loading = _this.$loading({
      text: "正在加载数据中...",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.5)"
    });
    let ajax = getAjax();
    ajax
      .get(`/api/download/url/${platform}/${hash}/${type}`, {
        headers: {
          secret: secretUtil.getSecret(platform, hash, type)
        }
      })
      .then(function(response) {
        if (response.data.code === 0) {
          _this.songName = songInfo.songName;
          _this.singerName = songInfo.singerName;
          _this.type = type === "flac" ? type : "mp3";
          _this.fileName =
            _this.singerName + " - " + _this.songName + "." + _this.type;
          _this.downloadUrl = response.data.data;
          _this.downloadDialogVisible = true;
        } else {
          _this.$message.error(response.data.message);
        }
        loading.close();
      })
      .catch(reason => {
        this.$message.error("请求失败");
        console.log(reason);
        loading.close();
      });
  }
};
